package com.zxz.demo1.controller;

import com.sun.deploy.net.HttpResponse;
import com.zxz.demo1.bean.Student;
import com.zxz.demo1.bean.User;
import com.zxz.demo1.dao.Stump;
import com.zxz.demo1.service.stuService;
import com.zxz.demo1.service.userService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
public class indexController {

    @Autowired
    userService userServiceimpl;
    @Autowired
    stuService stuService;



    @PostMapping("/login")
    String login(@RequestBody User user, HttpSession session, Model model){
//            System.out.println(user);
//        if(!StringUtils.isEmpty(user.getUsername())&&"admin".equals(user.getPassword())){
            session.setAttribute("loginUser",user);
            Class<? extends userService> aClass = userServiceimpl.getClass();
//            System.out.println(aClass);
            HashMap<String, Object> map = new HashMap<>();
            map.put("username",user.getUsername());
            map.put("password",user.getPassword());
            List<User> users = userServiceimpl.listByMap(map);
//            System.out.println(users);
            if (users.size()>0){
                return "登录成功";
            }else {
                model.addAttribute("msg","用户名或密码错误");
                return "用户名或密码错误";
            }
//        }else {
//            model.addAttribute("msg","用户名或密码错误");
//            return "login";
//        }
    }

    @GetMapping("/student")
    Object orderseach(){
        List<Student> list = stuService.list();
        for (Student student : list) {
            return list;
        }
        return "123";
    }

    @PostMapping("/update")
    String update(@RequestBody Student student){
        System.out.println(student);
        String sid = student.getSid();
        System.out.println(sid);
        boolean b = stuService.updateById(student);
        if(b){
            return "success";
        }else {
            return "error";
        }
    }

    @GetMapping("/delete/{id}")
    String delete(@PathVariable("id") String id){
        System.out.println(id);
        boolean b = stuService.removeById(id);
        if(b){
            return "success";
        }else {
            return "error";
        }


    }

    @PutMapping("/insert")
    String insertStu(@RequestBody Student student){
        System.out.println(student);
        boolean save = stuService.save(student);
        if(save){
            return "success";
        }else{
            return "error";
        }

    }

}
